<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends CI_Controller {
    public $pageName="article";
	public function __construct(){
		parent::__construct();//创造型函数
		$this->load->model('Catalog_model');
		$this->load->model('Article_model');
		if (!$this->session->userdata('is_login')) {
			redirect('admin/auth/login');//父容器元素。子集集成
		}
	}

	public function index($page=1){
		$this->load->model('Article_model');
		$totals=$this->Article_model->count_all();
		$per_page=5;	             //设定每页显示10个，
		$articles=$this->Article_model->get_by_page($page,$per_page);             //调用MODEL层的方法然后拿到数据
		$data = array(
			'articles' =>$articles                      //拿到的数据打包进$data
		);



		$this->load->library('pagination');
		$config['base_url'] = 'http://localhost/ciblog/index.php/admin/article/index';
		$config['total_rows'] = $totals;
		$config['per_page'] = $per_page;
		$this->pagination->initialize($config);
		$this->load->view('admin/Article_view',$data);
	}

	public function add_one(){
        $this->load->library('form_validation');               //加载该项才能用
        $this->form_validation->set_rules('title', '标题', 'required');          //限制条件，必填项
        $this->form_validation->set_rules('catalog_id', '分类', 'required');
        $this->form_validation->set_rules('content', '内容', 'required');
        $this->form_validation->set_rules('img', '图片', 'required');
        	$articles=new stdClass();                  //创建一个空的对象，进行欺骗
        	$articles->title="";
    		$articles->img="";
    		$articles->content="";
    		$articles->desc="";
    		$articles->catalog_id="";
    		$articles->recommend="";
    		$articles->view='';
        if ($this->form_validation->run() == FALSE){
        	$this->load->model('Catalog_model');
        	$data=array(
        		'catalogs'=>$this->Catalog_model->getAll(),
        		'title'=>"添加",
        		'articles'=>$articles
        	);
        	$this->load->view('admin/Article_form',$data);
        }else{
            $data=$this->input->post();           //拿到POST过来的数据，塞进数组里面。
            $this->load->model('Article_model');
            $this->Article_model->add_one($data);
            redirect('admin/article/index');
        }
	}

	public function edit_one($id){
        $this->load->library('form_validation');            //加载该项才能用
        $this->form_validation->set_rules('title', '标题', 'required');                //限制条件，必填项
        $this->form_validation->set_rules('catalog_id', '分类', 'required');
        $this->form_validation->set_rules('content', '内容', 'required');
        $this->form_validation->set_rules('img', '图片', 'required');


            $this->load->model('Article_model');
        if ($this->form_validation->run() == FALSE){
        	$this->load->model('Catalog_model');
        	$data=array(
        		'catalogs'=>$this->Catalog_model->getAll(),
        		'articles'=>$this->Article_model->getOne($id),
        		'title'=>'编辑'
        	);
        	$this->load->view('admin/Article_form',$data);
        }else{
            $data=$this->input->post();        //拿到POST过来的数据，塞进数组里面。
            $this->Article_model->edit_one($id,$data);
            //redirect('admin/article/index');
        }
	}

	public function del_one($id){
		$this->load->model('Article_model');
		$this->Article_model->getOne($id);
		$this->Article_model->del_one($id);
		//redirect('admin/article/index');
	}

	public function rec_one($id){
		$this->load->model('Article_model');
		$this->Article_model->rec_one($id);
		//redirect('admin/article/index');
	}
} 	